package com.example.sample.controller;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.*;

@RestController()
@RequestMapping("/services")
public class ServiceDiscoveryController {

    @GetMapping("/register")
    public String Register() throws NacosException {

        NamingService naming = NamingFactory.createNamingService("1.117.193.193:8848");
        Instance instance = new Instance();
        instance.setIp("55.55.55.55");
        instance.setPort(9999);
        instance.setHealthy(false);
        instance.setWeight(2.0);
        Map<String, String> instanceMeta = new HashMap<>();
        instanceMeta.put("site", "et2");
        instance.setMetadata(instanceMeta);

        Instance instance2 = new Instance();
        instance2.setIp("66.66.66.66");
        instance2.setPort(9999);
        instance2.setHealthy(false);
        instance2.setWeight(2.0);
        Map<String, String> instanceMeta2 = new HashMap<>();
        instanceMeta2.put("site", "et2666");
        instance2.setMetadata(instanceMeta2);

        naming.registerInstance("nacos.test.4", instance);
        naming.registerInstance("nacos.test.4", instance2);
        naming.registerInstance("test5", instance2);

        return "Hello World";
    }

    //注销实例
    @GetMapping("/deregister")
    public void Deregister() throws NacosException {
        NamingService namingService = NamingFactory.createNamingService("1.117.193.193:8848");
        namingService.deregisterInstance("nacos.test.4","66.66.66.66",9999);
    }

    //获取全部实例
    @GetMapping("/all")
    public List<Instance>  getAllInstances() throws NacosException {
        NamingService namingService = NamingFactory.createNamingService("1.117.193.193:8848");
        List<Instance> allInstances = namingService.getAllInstances("nacos.test.4");
        return  allInstances;
    }

    @GetMapping("/sub")
    public void  Subscribe() throws NacosException {
        NamingService namingService = NamingFactory.createNamingService("1.117.193.193:8848");
        EventListener eventListener =new EventListener() {
            @Override
            public void onEvent(Event event) {
                System.out.println(((NamingEvent)event).getServiceName());
                System.out.println(((NamingEvent)event).getInstances());

            }
        };
        namingService.subscribe("nacos.test.4",eventListener);
    }
}
